import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:mtf/R.dart';
import 'package:mtf/app/data/WithdrawRecordEntity.dart';
import 'package:mtf/app/modules/withdraw_record_module/withdraw_record_controller.dart';
import 'package:mtf/app/theme/app_boxDecoration.dart';
import 'package:mtf/app/theme/app_colors.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import '../../utils/AppUtils.dart';
/**
 * GetX Template Generator - fb.com/htngu.99
 * */

class withdrawRecordPage extends GetView<withdrawRecordController> {
  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 4,
      child: Scaffold(
        appBar: AppBar(
          elevation: 0,
          centerTitle: true,
          leading: IconButton(
              icon: Icon(
                Icons.arrow_back_ios,
                color: Colors.white,
                size: 22,
              ),
              onPressed: () => Navigator.pop(context)),
          title: Text("提现记录"),
          bottom: PreferredSize(
            preferredSize: Size.fromHeight(45),
            child: Material(
              color: Colors.white,
              child: TabBar(
                indicatorColor: c_f12f38,
                // 标签栏中选中的线（指示器）的颜色
                indicatorSize: TabBarIndicatorSize.label,
                // 线的长度
                indicatorWeight: 3.0,
                // 线的宽度
                labelColor: c_333333,
                unselectedLabelColor: c_666666,
                tabs: [
                  Tab(text: "全部"),
                  Tab(text: "审核中"),
                  Tab(text: "已通过"),
                  Tab(text: "已驳回"),
                ],
              ),
            ),
          ),
        ),
        body: TabBarView(
          children: withdrawPages(),
        ),
      ),
    );
  }

  List<Widget> withdrawPages() {
    var state = [-1, 0, 1, 2];
    List<Widget> pages = [];
    for (int i = 0; i < 4; i++) {
      RefreshController refreshController =
          RefreshController(initialRefresh: true);
      pages.add(RefreshConfiguration(
        headerBuilder: () => ClassicHeader(),
        footerBuilder: () => ClassicFooter(),
        child: SmartRefresher(
          controller: refreshController,
          enablePullDown: true,
          enablePullUp: true,
          onRefresh: () {
            controller.refreshData(refreshController, state[i]);
          },
          onLoading: () {
            controller.loadData(refreshController, state[i]);
          },
          child: Obx(() {
            return controller.getList(state[i]).length < 1? Align(
              alignment: Alignment.center,
              child: Visibility(
                  visible: controller.getList(state[i]).length < 1,
                  child: Column(
                    children: [
                      Image.asset(
                        R.assetsImgBgEmpty,
                        height: 150,
                        width: 150,
                      ),
                      Text("暂无记录")
                    ],
                  )),
            ):  Container(
              margin: EdgeInsets.all(10),
              decoration: r10WhiteDecoration,
              child: ListView.separated(
                  physics: NeverScrollableScrollPhysics(),
                  separatorBuilder: (context, index) {
                    return Container(
                        margin: EdgeInsets.symmetric(horizontal: 15),
                        child: Divider(height: 1));
                  },
                  itemBuilder: (context, index) {
                    return withdrawItem(
                        controller.getList(state[i])[index], i);
                  },
                  itemCount: controller.getList(state[i]).length,
                  shrinkWrap: true),
            );
            return Stack(
              children: [
                Align(
                  alignment: Alignment.center,
                  child: Visibility(
                      visible: controller.getList(state[i]).length < 1,
                      child: Column(
                        children: [
                          Image.asset(
                            R.assetsImgBgEmpty,
                            height: 150,
                            width: 150,
                          ),
                          Text("暂无记录")
                        ],
                      )),
                ),
                Container(
                  margin: EdgeInsets.all(10),
                  decoration: r10WhiteDecoration,
                  child: ListView.separated(
                      physics: NeverScrollableScrollPhysics(),
                      separatorBuilder: (context, index) {
                        return Container(
                            margin: EdgeInsets.symmetric(horizontal: 15),
                            child: Divider(height: 1));
                      },
                      itemBuilder: (context, index) {
                        return withdrawItem(
                            controller.getList(state[i])[index], i);
                      },
                      itemCount: controller.getList(state[i]).length,
                      shrinkWrap: true),
                ),
              ],
            );
          }),
        ),
      ));
    }
    return pages;
  }

  Widget withdrawItem(ListWithdrawElement bean, int index) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 10, vertical: 15),
      width: double.infinity,
      child: Column(
        children: [
          Flex(
            direction: Axis.horizontal,
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                "提现至尾号${bean.bank?.bankEndAccount()}的${bean.bank?.bankName ?? ""}",
                style: TextStyle(color: c_333333, fontSize: 15),
              ),
              Text(
                bean.amount.toString()+"USD" ?? "",
                style: TextStyle(color: c_333333, fontSize: 15),
              ),
            ],
          ),
          SizedBox(
            height: 5,
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                "预计到账￥${bean.rateAmount}",
                style: TextStyle(color: c_333333, fontSize: 12),
              ),
              Text(
                bean.getStatusValue(),
                style: TextStyle(color: c_f12f38, fontSize: 12),
              )
            ],
          ),
          SizedBox(
            height: 5,
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                AppUtils.dateToString(bean.createdAt) ?? "",
                style: TextStyle(color: c_999999, fontSize: 12),
              ),
              // Text(
              //   bean.getStatusValue(),
              //   style: TextStyle(color: c_f12f38, fontSize: 12),
              // )
            ],
          ),
          Visibility(
            visible: index == 3,
            child: Container(
              margin: EdgeInsets.only(top: 10),
              padding: EdgeInsets.all(10),
              width: double.infinity,
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(4), color: c_f4f4f4),
              child: Text.rich(TextSpan(children: [
                TextSpan(
                    text: "驳回原因：",
                    style: TextStyle(color: Color(0xff407FDC), fontSize: 12)),
                TextSpan(
                    text: bean.checkNote ?? "",
                    style: TextStyle(color: c_666666, fontSize: 12))
              ])),
            ),
          )
        ],
      ),
    );
  }
}
